package cn.schoolwow.quickdao.module.database.dml.parent.kit;

import cn.schoolwow.quickdao.module.database.dml.common.domain.GetBatchParametersSupplier;
import cn.schoolwow.quickdao.module.database.parent.flow.executor.ExecuteBatchUpdateConnectionFlow;
import cn.schoolwow.quickdao.module.database.parent.flow.executor.ExecuteUpdateConnectionFlow;
import cn.schoolwow.quickdao.module.database.parent.kit.DatabaseDAOImpl;
import cn.schoolwow.quickflow.QuickFlow;
import cn.schoolwow.quickflow.domain.FlowContext;

import java.util.Arrays;
import java.util.List;

public class DatabaseManipulationParentImpl extends DatabaseDAOImpl implements DatabaseManipulationParent {
    public DatabaseManipulationParentImpl(QuickFlow quickFlow) {
        super(quickFlow);
    }

    @Override
    public int rawUpdate(String updateSQL, Object... parameters) {
        FlowContext flowContext = quickFlow.startFlow(new ExecuteUpdateConnectionFlow())
                .putTemporaryData("name", "用户自定义更新")
                .putTemporaryData("sql", updateSQL)
                .putTemporaryData("parameters", Arrays.asList(parameters))
                .execute();
        int effect = (int) flowContext.checkData("effect");
        return effect;
    }

    @Override
    public int rawUpdateBatch(String updateSQL, List<List<Object>> parameterBatchList) {
        FlowContext flowContext = quickFlow.startFlow(new ExecuteBatchUpdateConnectionFlow())
                .putTemporaryData("name", "用户自定义批处理")
                .putTemporaryData("sql", updateSQL)
                .putTemporaryData("size", parameterBatchList.size())
                .putTemporaryData("getBatchParametersSupplier",new GetBatchParametersSupplier() {
                    @Override
                    public List<Object> getBatchParameters(Integer index) throws Exception {
                        return parameterBatchList.get(index);
                    }
                })
                .execute();
        int effect = (int) flowContext.checkData("effect");
        return effect;
    }

}
